import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        mainLayout = QVBoxLayout()

        toolBar = QToolBar()
        
        btn = QPushButton('Add Top')
        btn.clicked.connect(self.OnAddTopClicked)
        toolBar.addWidget(btn)

        btn = QPushButton('Add Child')
        btn.clicked.connect(self.OnAddChildClicked)
        toolBar.addWidget(btn)        

        btn = QPushButton('Delete')
        btn.clicked.connect(self.OnDelClicked)
        toolBar.addWidget(btn)

        toolBar.addWidget(QLabel('Update:'))
        self.currentEdit = QLineEdit()
        toolBar.addWidget(self.currentEdit)
        btn = QPushButton('Update')
        btn.clicked.connect(self.OnUpdateClicked)
        toolBar.addWidget(btn) 

        self.treeView = QTreeView()
        self.treeData = QStandardItemModel()
        self.treeView.setModel(self.treeData)
        self.treeData.setHorizontalHeaderLabels(['ID','Value'])        
        mainLayout.addWidget(toolBar)
        mainLayout.addWidget(self.treeView)
        self.setLayout(mainLayout) 
    
    def OnAddTopClicked(self):
        id = random.randint(0, 999)
        txt = f'Node{id}'
        self.treeData.appendRow(QStandardItem(txt))
        self.parent.OnShowMessage(f"Add top item '{txt}'")
    def OnAddChildClicked(self):
        id = random.randint(0, 999)
        arr = [f'{id}',f'Node{id}']        
        currentIndex = self.treeView.currentIndex()        
        currentItem = self.treeData.itemFromIndex(currentIndex)
        childCount = currentItem.rowCount()
        #currentItem.setChild(childCount,0,QStandardItem(arr[0]))
        #currentItem.setChild(childCount,1,QStandardItem(arr[1]))    
        currentItem.appendRow([QStandardItem(arr[0]),QStandardItem(arr[1])]) 
        self.treeView.expand(currentIndex)#展开            
        self.parent.OnShowMessage(f"Add child item '{arr[1]}'")                 
    
    def OnDelClicked(self):
        currentIndex = self.treeView.currentIndex()
        #print(f'row:{current.row()},column:{current.column()}')
        currentItem =self.treeData.itemFromIndex(currentIndex)
        txt =currentItem.text()
        if currentItem.parent() is None:
            self.treeData.removeRow(currentIndex.row())  
        else:
            currentItem.parent().removeRow(currentIndex.row())      
        self.parent.OnShowMessage(f"Delete item '{txt}'")
            
    def OnUpdateClicked(self):
        currentIndex = self.treeView.currentIndex()        
        currentItem = self.treeData.itemFromIndex(currentIndex)
        old = currentItem.text()
        new = self.currentEdit.text()
        currentItem.setText(new)
        self.parent.OnShowMessage(f"Update item from '{old}' to '{new}'")
        
    
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet